﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 通用设备的基础网络信息
	/// </summary>
	// Token: 0x020000F2 RID: 242
	public class DeviceNet : NetworkServerBase
	{
		/// <summary>
		/// 实例化一个通用的设备类
		/// </summary>
		// Token: 0x0600148B RID: 5259 RVA: 0x0006A733 File Offset: 0x00068933
		public DeviceNet()
		{
			this.list = new List<DeviceState>();
			this.lock_list = new SimpleHybirdLock();
		}

		// Token: 0x0600148C RID: 5260 RVA: 0x0006A75B File Offset: 0x0006895B
		private void AddClient(DeviceState device)
		{
			this.lock_list.Enter();
			this.list.Add(device);
			this.lock_list.Leave();
			Action<DeviceState> clientOnline = this.ClientOnline;
			if (clientOnline != null)
			{
				clientOnline(device);
			}
		}

		// Token: 0x0600148D RID: 5261 RVA: 0x0006A798 File Offset: 0x00068998
		private void RemoveClient(DeviceState device)
		{
			this.lock_list.Enter();
			this.list.Remove(device);
			Socket workSocket = device.WorkSocket;
			if (workSocket != null)
			{
				workSocket.Close();
			}
			this.lock_list.Leave();
			Action<DeviceState> clientOffline = this.ClientOffline;
			if (clientOffline != null)
			{
				clientOffline(device);
			}
		}

		/// <summary>
		/// 当客户端上线的时候，触发此事件
		/// </summary>
		// Token: 0x1400002A RID: 42
		// (add) Token: 0x0600148E RID: 5262 RVA: 0x0006A7F0 File Offset: 0x000689F0
		// (remove) Token: 0x0600148F RID: 5263 RVA: 0x0006A828 File Offset: 0x00068A28
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<DeviceState> ClientOnline;

		/// <summary>
		/// 当客户端下线的时候，触发此事件
		/// </summary>
		// Token: 0x1400002B RID: 43
		// (add) Token: 0x06001490 RID: 5264 RVA: 0x0006A860 File Offset: 0x00068A60
		// (remove) Token: 0x06001491 RID: 5265 RVA: 0x0006A898 File Offset: 0x00068A98
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<DeviceState> ClientOffline;

		/// <summary>
		/// 按照ASCII文本的方式进行触发接收的数据
		/// </summary>
		// Token: 0x1400002C RID: 44
		// (add) Token: 0x06001492 RID: 5266 RVA: 0x0006A8D0 File Offset: 0x00068AD0
		// (remove) Token: 0x06001493 RID: 5267 RVA: 0x0006A908 File Offset: 0x00068B08
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<DeviceState, string> AcceptString;

		/// <summary>
		/// 按照字节的方式进行触发接收的数据
		/// </summary>
		// Token: 0x1400002D RID: 45
		// (add) Token: 0x06001494 RID: 5268 RVA: 0x0006A940 File Offset: 0x00068B40
		// (remove) Token: 0x06001495 RID: 5269 RVA: 0x0006A978 File Offset: 0x00068B78
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<DeviceState, byte[]> AcceptBytes;

		/// <summary>
		/// 当接收到了新的请求的时候执行的操作
		/// </summary>
		/// <param name="socket">异步对象</param>
		/// <param name="endPoint">终结点</param>
		// Token: 0x06001496 RID: 5270 RVA: 0x0006A9B0 File Offset: 0x00068BB0
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			DeviceState deviceState = new DeviceState
			{
				WorkSocket = socket,
				DeviceEndPoint = (IPEndPoint)socket.RemoteEndPoint,
				IpAddress = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(),
				ConnectTime = DateTime.Now
			};
			this.AddClient(deviceState);
			try
			{
				deviceState.WorkSocket.BeginReceive(deviceState.Buffer, 0, deviceState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ContentReceiveCallBack), deviceState);
			}
			catch (Exception ex)
			{
				this.RemoveClient(deviceState);
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.NetClientLoginFailed, ex);
				}
			}
		}

		// Token: 0x06001497 RID: 5271 RVA: 0x0006AA7C File Offset: 0x00068C7C
		private void ContentReceiveCallBack(IAsyncResult ar)
		{
			DeviceState deviceState = ar.AsyncState as DeviceState;
			bool flag = deviceState != null;
			if (flag)
			{
				try
				{
					int num = deviceState.WorkSocket.EndReceive(ar);
					bool flag2 = num > 0;
					if (flag2)
					{
						MemoryStream memoryStream = new MemoryStream();
						byte[] array;
						for (byte b = deviceState.Buffer[0]; b != this.endByte; b = array[0])
						{
							memoryStream.WriteByte(b);
							array = new byte[1];
							deviceState.WorkSocket.Receive(array, 0, 1, SocketFlags.None);
						}
						deviceState.WorkSocket.BeginReceive(deviceState.Buffer, 0, deviceState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ContentReceiveCallBack), deviceState);
						byte[] array2 = memoryStream.ToArray();
						memoryStream.Dispose();
						this.lock_list.Enter();
						deviceState.ReceiveTime = DateTime.Now;
						this.lock_list.Leave();
						Action<DeviceState, byte[]> acceptBytes = this.AcceptBytes;
						if (acceptBytes != null)
						{
							acceptBytes(deviceState, array2);
						}
						Action<DeviceState, string> acceptString = this.AcceptString;
						if (acceptString != null)
						{
							acceptString(deviceState, Encoding.ASCII.GetString(array2));
						}
					}
					else
					{
						this.RemoveClient(deviceState);
						ILogNet logNet = base.LogNet;
						if (logNet != null)
						{
							logNet.WriteInfo(this.ToString(), StringResources.Language.NetClientOffline);
						}
					}
				}
				catch (Exception ex)
				{
					this.RemoveClient(deviceState);
					ILogNet logNet2 = base.LogNet;
					if (logNet2 != null)
					{
						logNet2.WriteException(this.ToString(), StringResources.Language.NetClientLoginFailed, ex);
					}
				}
			}
		}

		// Token: 0x040004E2 RID: 1250
		private List<DeviceState> list;

		// Token: 0x040004E3 RID: 1251
		private SimpleHybirdLock lock_list;

		// Token: 0x040004E8 RID: 1256
		private readonly byte endByte = 13;
	}
}
